<template>

</template>

<script setup>
	import { getUrlParam } from '@/utils/h5/index';
	import request from '@/utils/common/request/index';
	import { useUserStore } from '@/store/modules/useUserStore';
	import { onLoad } from '@dcloudio/uni-app';

	let redirectUrl = '';
	onLoad(options => {
		redirectUrl = options.redirect;

		// * 如果有code触发登录
		const code = getUrlParam('code');
		if (code) {
			request({
				url: '/api/wxauth',
				method: 'POST',
				loadingTip: '登录中',
				data: {
					code,
					register_channel: 2
				}
			}).then(res => {
				uni.setStorageSync('token', res.token);
				// * 获取用户信息
				request({ url: '/api/uinfo' }).then(res => {
					const userStore = useUserStore();
					userStore.setData(res);
					uni.redirectTo({
						url: redirectUrl,
						fail() {
							uni.reLaunch({
								url: redirectUrl
							})
						}
					})
				});
			});
		};
	})
</script>

<style>

</style>